/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.tabbedproperties.section;


import org.eclipse.core.runtime.Assert;
import org.eclipse.gef.editparts.AbstractGraphicalEditPart;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.views.properties.tabbed.ITabbedPropertyConstants;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;

import com.cea.papyrus.core.tabbedproperties.PropertyViewSection;
import com.cea.papyrus.core.utils.Debug;
import com.cea.papyrus.diagraminterchange2.di2.Di2Factory;
import com.cea.papyrus.diagraminterchange2.di2.GraphElement;
import com.cea.papyrus.umlutils.ICustomAppearence;

/**
 * Section to customize mutliplicity display for a property when this property
 * is displayed as a relation end of an association.
 * 
 * @author Remi Schnekenburger
 */
public class PropertyRelationEndLabelAppearenceSection extends PropertyViewSection {
	
	/**
	 * 
	 */
	private GraphElement graphElement;
	
	// there should be several buttons
	// the first one indicates if the element is visible or not
	// if it is visible, label can be customized
	// name, visibility, modifiers
	/**
	 * 
	 */
	private Button displIsVisible;
	
	/**
	 * 
	 */
	private Button displName, displVisibility, displModifiers, displMultiLine;
	
	/**
	 * 
	 */
	private SelectionListener displIsVisibleListener, displNameListener, displVisibilityListener, displModifiersListener, displMultiLineListener;


	/* (non-Javadoc)
	 * @see org.eclipse.ui.views.properties.tabbed.AbstractPropertySection#createControls(org.eclipse.swt.widgets.Composite, org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage)
	 */
	/**
	 * 
	 * 
	 * @param tabbedPropertySheetPage 
	 * @param parent 
	 */
	public void createControls(Composite parent, TabbedPropertySheetPage tabbedPropertySheetPage) {
		super.createControls(parent, tabbedPropertySheetPage);
		Composite composite = getWidgetFactory().createFlatFormComposite(parent);
		FormData data;

		displIsVisible = getWidgetFactory().createButton(composite, "Visible", SWT.CHECK);
		data = new FormData();
		data.left = new FormAttachment(0, 0);
		data.top = new FormAttachment(0, 0);
		displIsVisible.setLayoutData(data);
		displIsVisible.addSelectionListener(displIsVisibleListener = new IsVisibleSelectionListener());
		
		// now, buttons for custom label (vertical layout?)
		displVisibility = getWidgetFactory().createButton(composite, "Visibility", SWT.CHECK);
		data = new FormData();
		data.left = new FormAttachment(displIsVisible, 85);
		data.top = new FormAttachment(0, 0);
		displVisibility.setLayoutData(data);
		displVisibility.addSelectionListener(displVisibilityListener = new AppearenceSelectionListener(ICustomAppearence.DISP_VISIBILITY));
		
		displName = getWidgetFactory().createButton(composite, "Name", SWT.CHECK);
		data = new FormData();
		data.left = new FormAttachment(displIsVisible, 85);
		data.top = new FormAttachment(displVisibility, ITabbedPropertyConstants.HSPACE);
		displName.setLayoutData(data);
		displName.addSelectionListener(displNameListener = new AppearenceSelectionListener(ICustomAppearence.DISP_NAME));
	
		displModifiers = getWidgetFactory().createButton(composite, "Modifiers", SWT.CHECK);
		data = new FormData();
		data.left = new FormAttachment(displVisibility, 85);
		data.top = new FormAttachment(0, 0);
		displModifiers.setLayoutData(data);
		displModifiers.addSelectionListener(displModifiersListener = new AppearenceSelectionListener(ICustomAppearence.DISP_MOFIFIERS));
	
		displMultiLine = getWidgetFactory().createButton(composite, "Modifiers on several lines", SWT.CHECK);
		data = new FormData();
		data.left = new FormAttachment(displVisibility, 85);
		data.top = new FormAttachment(displModifiers, ITabbedPropertyConstants.HSPACE);
		displMultiLine.setLayoutData(data);
		displMultiLine.addSelectionListener(displMultiLineListener = new AppearenceSelectionListener(ICustomAppearence.DISP_MULTI_LINE));
	}

	/*
	 * @see org.eclipse.ui.views.properties.tabbed.view.ITabbedPropertySection#refresh()
	 */
	/**
	 * 
	 */
	public void refresh() {
		if(!displIsVisible.isDisposed()){
			
			// if no graph element are associated, there should not be the appearence section
			if(graphElement == null) {
				return;
			}
			// get Property associated to this element
			displIsVisible.setSelection(graphElement.isIsVisible());
			
			// if graph element is visible, customize appearence
			if(!graphElement.isIsVisible()) {
				displVisibility.setEnabled(false);
				displName.setEnabled(false);
				displModifiers.setEnabled(false);
				displMultiLine.setEnabled(false);
			} else {
				//set enable to true
				displVisibility.setEnabled(true);
				displName.setEnabled(true);
				displModifiers.setEnabled(true);
				displMultiLine.setEnabled(true);

				// get custom label property
				com.cea.papyrus.diagraminterchange2.di2.Property prop = graphElement.getProperty(ICustomAppearence.CUSTOM_APPEARENCE_KEY);
				if(prop == null) {
					// creates a default one
					prop = Di2Factory.eINSTANCE.createProperty();
					graphElement.getProperty().add(prop);
					prop.setKey(ICustomAppearence.CUSTOM_APPEARENCE_KEY);
					prop.setValue(""+ICustomAppearence.DEFAULT_UML_RELATIONEND_PROPERTY);
				}
				int value = Integer.parseInt(prop.getValue());

				// modifiy state depending on the property of the node
				displVisibility.setSelection((value & ICustomAppearence.DISP_VISIBILITY) != 0);
				displName.setSelection((value & ICustomAppearence.DISP_NAME) != 0);
				displModifiers.setSelection((value & ICustomAppearence.DISP_MOFIFIERS) != 0);
				displMultiLine.setSelection((value & ICustomAppearence.DISP_MULTI_LINE) != 0);
			}
		}
	}
	
	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.ui.properties.tabbed.PropertyViewSection#setInput(org.eclipse.ui.IWorkbenchPart, org.eclipse.jface.viewers.ISelection)
	 */
	/**
	 * 
	 * 
	 * @param part 
	 * @param selection 
	 */
	public void setInput(IWorkbenchPart part, ISelection selection) {
		super.setInput(part, selection);
		Assert.isTrue(selection instanceof IStructuredSelection);
		Object input = ((IStructuredSelection) selection).getFirstElement();

		if(input instanceof AbstractGraphicalEditPart) {
			graphElement = ((GraphElement)((AbstractGraphicalEditPart) input).getModel());
		} else {
			graphElement = null;
		}
		refresh();
	}

	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.ui.properties.tabbed.PropertyViewSection#dispose()
	 */
	/**
	 * 
	 */
	public void dispose(){
		super.dispose();
		if(displIsVisible!=null && !displIsVisible.isDisposed())
			displIsVisible.removeSelectionListener(displIsVisibleListener);
		if(displName!=null && !displName.isDisposed())
			displName.removeSelectionListener(displNameListener);
		if(displVisibility!=null && !displVisibility.isDisposed())
			displVisibility.removeSelectionListener(displVisibilityListener);
		if(displModifiers!=null && !displModifiers.isDisposed())
			displModifiers.removeSelectionListener(displModifiersListener);
		if(displMultiLine!=null && !displMultiLine.isDisposed())
			displMultiLine.removeSelectionListener(displMultiLineListener);
	}
	
	/**
	 * 
	 */
	private class IsVisibleSelectionListener implements SelectionListener {

		/* (non-Javadoc)
		 * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent)
		 */
		/**
		 * 
		 * 
		 * @param e 
		 */
		public void widgetDefaultSelected(SelectionEvent e) {

		}

		/* (non-Javadoc)
		 * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
		 */
		/**
		 * 
		 * 
		 * @param e 
		 */
		public void widgetSelected(SelectionEvent e) {
			Button button = (Button)e.getSource();
			boolean isSelected = button.getSelection();

			if(graphElement == null) {
				return;
			}
			graphElement.setIsVisible(isSelected);
			forceToSave();
			refresh();
		}
	}
	
	/**
	 * 
	 */
	private class AppearenceSelectionListener implements SelectionListener {

		/**
		 * the constant which refers the element to display or not.
		 */
		private int style;

		/**
		 * Default Constructor.
		 * 
		 * @param style 
		 */
		public AppearenceSelectionListener(int style) {
			this.style = style;
		}

		/* (non-Javadoc)
		 * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent)
		 */
		/**
		 * 
		 * 
		 * @param e 
		 */
		public void widgetDefaultSelected(SelectionEvent e) {

		}

		/* (non-Javadoc)
		 * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
		 */
		/**
		 * 
		 * 
		 * @param e 
		 */
		public void widgetSelected(SelectionEvent e) {
			Button button = (Button)e.getSource();
			boolean isSelected = button.getSelection();

			if(graphElement == null) {
				return;
			}
			com.cea.papyrus.diagraminterchange2.di2.Property prop = graphElement.getProperty(ICustomAppearence.CUSTOM_APPEARENCE_KEY);
			if(prop == null) {
				Debug.error(null, "property is null", 7);
			}
			int value = Integer.parseInt(prop.getValue());

			if(isSelected) {
				if((value & style) == 0) {
					prop.setValue(""+(value | style));
				}
			} else {
				if((value & style) != 0) {
					prop.setValue(""+(value & ~style));
				}
			}
			forceToSave();
			refresh();
		}
	}

}

